//
//  NearbyViewController.m
//  BaiTuan
//
//  Created by 看着太阳吃烧饼 on 15/12/13.
//  Copyright © 2015年 YR. All rights reserved.
//

#import "NearbyViewController.h"
#import "NearbyTableView.h"
#import "MenuView.h"
#import "ShopsModel.h"
#import "DealClassifyModel.h"
#import "DealModel.h"
#import "AppDelegate.h"
#import "SearchViewController.h"
#import "BaseNavController.h"

@interface NearbyViewController ()
{
    MenuView *_menu;
}
@property (nonatomic, strong) NearbyTableView *table;

@end

@implementation NearbyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //网络请求
    [self reloadData:nil];
    //创建视图
    [self createView];
    
}

#pragma mark - 创建视图

-(void)createView
{
    //创建menu
    if (_menu == nil) {
        
        _menu = [[MenuView alloc]initWithOrigin:CGPointMake(0, 0) andHeight:45];
        [self.view addSubview:_menu];
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:kClassifyChangeNotification object:nil];
    
    //创建tableView
    _table = [[NearbyTableView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight - 64 - 45 - 49) style:UITableViewStylePlain];
    
    //导航栏不透明
    self.navigationController.navigationBar.translucent = NO;
    _table.rowHeight = 80;
    [self.view addSubview:_table];
    
    
    self.title = @"全部商家";
    NSDictionary *dic = @{
                          NSForegroundColorAttributeName : [UIColor colorWithRed:45 / 255.0    green:177 / 255.0 blue:157 / 255.0 alpha:1]
                          };
    self.navigationController.navigationBar.titleTextAttributes = dic;
    
}

#pragma mark - 点击事件

- (IBAction)mapAction:(UIButton *)sender {
    
}

- (IBAction)searchAction:(UIButton *)sender {
    
    SearchViewController *search = [[SearchViewController alloc]init];
    [search setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:search animated:YES];
}
#pragma mark - 网络请求

- (void)reloadData:(NSNotification *)notification
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    [self requestShopData:delegate.userLocation subID:notification.userInfo[@"subID"]];
}

-(void)requestShopData:(CLLocation *)location subID:(NSNumber *)subID
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setValue:apikey forHTTPHeaderField:@"apikey"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    
    NSString *locationStr = [NSString stringWithFormat:@"%f,%f",location.coordinate.longitude,location.coordinate.latitude];

    if (subID != nil) {
        NSLog(@"%@",subID);
        [params setObject:subID forKey:@"subcat_ids"];
    }
    [params setObject:locationStr forKey:@"location"];
    [params setObject:@600010000 forKey:@"city_id"];
    

    [manager GET:shop_city parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self loadShopData:responseObject];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
    }];
    
}

-(void)requestDealClassifyData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setValue:apikey forKey:@"apikey"];
    
    [manager GET:deal_classify parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
    }];
}

-(void)loadShopData:(NSDictionary *)dic
{
    if (dic[@"data"] != [NSNull null])
    {
        NSDictionary *data = dic[@"data"];
        NSArray *shops = data[@"shops"];
        
        _shopArray = [[NSMutableArray alloc]init];
        for (NSDictionary *shopDic in shops) {
            
            ShopsModel *shopModel = [[ShopsModel alloc]initContentWithDic:shopDic];
            
            [_shopArray addObject:shopModel];
        }
        
        _table.dataArray = _shopArray;
        [_table reloadData];
    }
    
}

@end
